// pages/order/order/order.js
var handler = require("../../../utils/function.js");
var app = getApp(),
    api = require("../../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
      imgs: handler.userImg,
        img: handler.driverImg,
        order: [],
        left: 0,
        isPaid: 3,
        paid: '',
        speedFee: '',
        roadToll: '',
        otherFee: '',
        order_no: '',
        is_pay: 3,
        is_status:'未支付',
        pages:1,//当前页数
      order_url: [
        "/pages/driverOrder/orderedStatus/orderedStatus", 
        "/pages/driverOrder/orderGoing/orderGoing", 
        "/pages/driverOrder/bill/bill",
        "/pages/driverOrder/orderDetail/orderDetail",
        "/pages/driverOrder/orderedStatus/orderedStatus"
        ],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    // scrollHandler 滚动事件
    scrollHandler: function(e) {
        console.log('获取支付参数', e)
      var id = e.currentTarget.dataset.pay;
      this.setData({ isPaid: e.currentTarget.dataset.pay,is_pay: e.currentTarget.dataset.pay });
      this.data.pages = 1;
      this.getOrder();
    },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
      if (e.currentTarget.dataset.start == 4) {
        handler.intoPageHandler(e.currentTarget.dataset.skip + '?orderstart=1&order_no=' + e.currentTarget.dataset.order_no);
        return false;
      }
      handler.intoPageHandler(e.currentTarget.dataset.skip + '?order_no=' + e.currentTarget.dataset.order_no)
    },
    // 催款页面点击
    intoBill:function(e){
      var order_no = e.currentTarget.dataset.order_no;
      let that=this;
      wx.showLoading()
      app.request({
        url: api.order.dept,
        method: 'post',
        data: {
          
          order_no: order_no
        },
        success:function(e){
          wx.hideLoading()
          that.setData({ isPaid: 1, is_pay: 1 })
          that.getOrder();
          if (e.code){
            wx.showToast({
              title: e.msg,
            })
          }else{
            wx.showToast({
              title: e.msg,
            })
          }
        },
      })
    },
    onLoad: function(options) {
      // 获取优惠券
      if (options.uid) {
        app.shareCouponsMessage(options.uid)
      }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.data.pages = 1;
        this.getOrder();
    },
    getOrder:function(){
        var that = this;
        wx.showLoading({})
        app.request({
            url: api.order.order_status,
            method: 'post',
            data: {
              is_pay: that.data.is_pay, p: 10
            },
            success: function(rs) {
              wx.hideLoading()
                that.setData({
                    order: rs.data.data,
                    total: rs.data.total
                })
            }
        })
    },
    // 取消订单
    cancleOrder:function(e){
      var order_no = e.currentTarget.dataset.order_no
      wx.navigateTo({
        url: '/pages/driver/deleteOrderMasker/index?order_no=' + order_no ,
      })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        // 显示加载图标  
       wx.showNavigationBarLoading(); 
        let that = this;
        let pages = this.data.pages;
        let order = this.data.order;
        let count = Math.ceil(this.data.total / 10);
        pages++;
        if (pages++ >count) {
          wx.showToast({
            title: "没有更多数据了",
            icon: 'none'
          });
          return false;
        }
        // const { order}=this.data;
        app.request({
            url: api.order.order_status,
            method: 'post',
            data: {
                is_pay: that.data.is_pay,
                page: pages,
                p:10
            },
            success: function(rs) {
              if (rs.data.data.length!==0){
              for (var i = 0; i < rs.data.data.length;i++){
                order.push(rs.data.data[i])
              }
              }
                that.setData({
                  order: order,
                  pages: pages
                })
                 wx.hideNavigationBarLoading()
            }
        })
    },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})